package mest.controller.hank;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import mest.VO.hank.dataTraffic_model.plan_takematerial_Model;
import mest.VO.hank.model.save_fileModel;
import mest.entity.DictionariesOrder;
import mest.entity.plan_productjob_saleout;
import mest.entity.plan_productjob_takematerial;
import mest.entity.plan_takematerial;
import mest.service.cxj.DictionariesOrderService;
import mest.service.hank.PlanProductjobSaleoutService;
import mest.service.hank.PlanProductjobTakematerialService;
import mest.service.hank.PlanTakematerialService;
import mest.service.hws.BomSaveManytomanyService;
import mest.util.DateUtil;
import mest.util.ResultObj;
import mest.util.hankUtil.Util_Code;
import mest.util.hankUtil.Util_CodeFactory;
import mest.util.mestUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname PlanTakeMaterial
 * @Description TODO
 * @Date 2019/11/30 14:21
 * @Created by 18416
 * @Auther: henk
 */

@RestController
@RequestMapping("/PlanTakeMaterial")
@CrossOrigin
public class PlanTakeMaterialController {
    //本类
    @Resource
    private PlanTakematerialService pts;
    //流水号表
    @Resource
    private DictionariesOrderService dos;
    //计划领料单
    private PlanProductjobTakematerialService ppts;
    //查物料和产品
    @Resource
    private BomSaveManytomanyService bsmts;
    /**
     * 需要循环插入的对象接口
     */
    @Resource
    private PlanProductjobTakematerialService pptms;


    /**
     * 分页查询+加过滤条件
     *
     * @param index     第几页
     * @param pageSize  页数
     * @param htmlModel 模糊查询字段
     * @param state     状态
     * @return
     */
    @RequestMapping("/queryAllModel")
    public Object queryAllModel(@RequestParam Integer index,
                                @RequestParam Integer pageSize,
                                String htmlModel,
                                @RequestParam Integer state, HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Long start = System.currentTimeMillis();
        try {
            String theirDb = mestUtil.getCompanyDB(request);
            List<Map> eModel = pts.queryAllModel(index,
                    pageSize, htmlModel, state,theirDb);

            if (!ObjectUtils.isEmpty(eModel)) {
                modelMap.put("data", eModel);
                modelMap.put("msg", "success");
                modelMap.put("code", 0);
                modelMap.put("count", pts.findCount(htmlModel,state,theirDb));
                modelMap.put("耗时/ms:", (System.currentTimeMillis() - start));
            } else {
                modelMap.put("msg", "此对象为空值");
            }

        } catch (Exception e) {
        	e.printStackTrace();
            modelMap.put("msg", "fales");
            modelMap.put("errmsg", e.toString());
        }
        return JSON.toJSONString(modelMap);
    }

    /**
     * 强制完单
     *
     * @param record
     * @return
     */
    @RequestMapping("/changeStatus")
    public Object changeStatus(@RequestBody JSONObject json, HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
        	Map<String, Object> map = json.toJavaObject(Map.class);
            map.put("theirDb", mestUtil.getCompanyDB(request));
            int i = pts.updateByMap(map);
            if (i > 0) {
                modelMap.put("data", i);
                modelMap.put("msg", Util_Code.Success_CODE);
                modelMap.put("code", 0);
            }
            return modelMap;
        } catch (Exception e) {
        	e.printStackTrace();
            modelMap.put("msg", Util_Code.Fail_CODE);
            modelMap.put("errmsg", e.toString());
            return modelMap;
        } 

        
    }

    /**
     * 1.新增
     * 2.给个接口进来果魔军用来插一插
     * url /PlanTakeMaterial/createModel_PlanTakeMaterial
     *
     * @param poId
     * @return
     * @throws ParseException
     */
    @RequestMapping("/createModel_PlanTakeMaterial")
    public Object createModel_PlanTakeMaterial(@RequestBody JSONObject json, HttpServletRequest request) throws ParseException {
    	Map<String,Object> map = json.toJavaObject(Map.class);
    	map.put("theirDb", mestUtil.getCompanyDB(request));
    	Long start = System.currentTimeMillis();
        //String theirDb = mestUtil.getCompanyDB(request);
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //2.判断 传入ID是否为空
        if (!ObjectUtils.isEmpty(map.get("poId"))) {
            try {
            	//判断是否有物料
            	int i = pts.existBom(map);
            	if (i < 1) {
					return ResultObj.createSuccessMsg("没有物料无须生成");
				}
                //4. 新建订单编号
                DictionariesOrder dictionaries_order = dos.selectDictionariesOrder(mestUtil.getCompanyDB(request));
                //4.(1).把结果查出来
                String number = dictionaries_order.getPlanTakeMaterialNumber().toString();

                //5.把对象插入表,并返回ID
                map.put("time", DateUtil.getDateTimeToDate());
                map.put("finishStatus", 0);
                map.put("tableNumber", Util_CodeFactory.create_plan_takematerial_CODE(number));
                
                int result_id = pts.insertResult_id(map);
                if (ObjectUtils.isEmpty(result_id)) {
                    modelMap.put("code", Util_Code.number_Fail);
                    modelMap.put("msg", Util_Code.Fail_CODE);
                } else {
                    //6.如果成功的情况下..  把结果返回给前端
                    modelMap.put("data", result_id);
                    modelMap.put("msg", Util_Code.Success_CODE + " 此表的ID是:" + result_id);
                    modelMap.put("code", Util_Code.number_Success);
                    modelMap.put("count", "model");
                    modelMap.put("ptId",result_id);
                    //7.将生成编号+1	
                    int i1 = dos.add_dictionaries_Order(mestUtil.getCompanyDB(request), "planTakeMaterialNumber");
                    List<plan_productjob_takematerial> ppjtlist = bsmts.selectAllModel3_takeMaterial(mestUtil.getCompanyDB(request),Integer.valueOf(map.get("poId")+""));
                    for (plan_productjob_takematerial list : ppjtlist) {
                        list.setPtId(result_id);
                    }
                    pptms.intsertList(ppjtlist,mestUtil.getCompanyDB(request));
                    modelMap.put("pptms", "插入成功");
                    modelMap.put("耗时/ms:", (System.currentTimeMillis() - start));
                }
            } catch (Exception e) {
            	e.printStackTrace();
                modelMap.put("code", Util_Code.number_Fail);
                modelMap.put("msg", Util_Code.Fail_CODE);
                modelMap.put("errmsg", e.toString());
            }
        }
        return JSON.toJSONString(modelMap);
    }


    @RequestMapping("/test")
    public Object test( HttpServletRequest request){
        System.out.println("我进来了");
        int i = dos.add_dictionaries_Order("planTakeMaterialNumber",mestUtil.getCompanyDB(request));
        System.out.println(i);
        if (i>0){
            System.out.println("成功");
        }
        return i;
    }

}
